package com.tianji.learning.service.impl;

import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.common.utils.ObjectUtils;
import com.tianji.learning.domain.po.PointsBoardSeason;
import com.tianji.learning.mapper.PointsBoardSeasonMapper;
import com.tianji.learning.service.IPointsBoardSeasonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 东哥
 * @since 2025-10-29
 */
@Slf4j
@Service
public class PointsBoardSeasonServiceImpl extends ServiceImpl<PointsBoardSeasonMapper, PointsBoardSeason> implements IPointsBoardSeasonService {

    /**
     * 根据时间查询所属的赛季ID
     *
     * @param lastMonthDate
     * @return
     */
    @Override
    public Integer querySeasonIdByDate(LocalDate lastMonthDate) {
        // 已知 begin end  ->  2025-10-01  2025-10-31
        // select * from `interaction_question` where create_time >= '2025-10-01' and create_time <= '2025-10-31'

        // 已知 date ->  2025-10-01
        // select * from points_board_season where begin_time <= '2025-10-01' and end_time >= '2025-10-01'
        PointsBoardSeason season = lambdaQuery()
                .le(PointsBoardSeason::getBeginTime, lastMonthDate)
                .ge(PointsBoardSeason::getEndTime, lastMonthDate)
                .one();
        if(ObjectUtils.isNull(season)){
            log.error("指定日期对应的赛季信息不存在：{}", lastMonthDate);
            throw new BizIllegalException("指定日期对应的赛季信息 : {" + lastMonthDate + "}不存在");
        }
        return season.getId();
    }
}
